# Create your views here.
# coding=utf-8
from django.template import Context, loader
from django.http import HttpResponse
from ltyxapp.models import *
#from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.shortcuts import RequestContext


class EmployeeForm(ModelForm):
    class Meta:
        model = Employee

def index(request): 
    notice=Nav.objects.get(title=u'学校公告')
    blog=Blog.objects.all()[:10]
    navsorts=NavSort.objects.all()
    navs=Nav.objects.all()
    recentlyannounced=Article.objects.filter(blog_nav=notice.id)
    grade=Grade.objects.all()
    t = loader.get_template('index.html')
    c = Context({
                 'navsorts':navsorts,
                 'navs':navs,
                 'notice':notice,
                 'recentlyannounced':recentlyannounced,
                 'grade':grade,
                 'blog':blog,
    })
    return HttpResponse(t.render(c))

print '----p-p-p' 
def register(request):
    if request.method=='GET':
        t = loader.get_template('register.html')
        c = Context({
                     
        })
        return HttpResponse(t.render(c))
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')  
        nickname=request.POST.get('nickname')  
        sex=request.POST.get('sex')  
        birthday=request.POST.get('birthday')  
        address=request.POST.get('address')  
        employee_group=request.POST.get('employee_group')  
        phone=request.POST.get('phone')          
        job=request.POST.get('job')  
        period=request.POST.get('period')  
        banji=request.POST.get('banji')  

        student=Employee()
        student.username=username
        student.password=password
        student.nickname=nickname
        student.sex=sex
        student.birthday=birthday
        student.address=address
        empgroup=EmployeeGroup.objects.get(employee_type=u'学生')
        student.employee_group=empgroup
        student.phone=phone
        student.period=period
        student.banji=banji
        student.save()
        print 'save emp ok'
        return HttpResponse('注册成功，请耐心等待管理员通过')
        
def nav(request,id):   
    print id,'-------id'
    navsort=Nav.objects.get(id=id)
    article=Article.objects.filter(blog_nav=navsort)
    
    article=Article.objects.filter(blog_nav=navsort)
    
    t = loader.get_template('article.html')
    c = Context({
                 'navsort':navsort,
                 'articles':article,
    })
    return HttpResponse(t.render(c))

def grade(request,id):   
    grade=Grade.objects.get(id=id)
    t = loader.get_template('grade.html')
    c = Context({
                 'grade':grade,
    })
    return HttpResponse(t.render(c))

def bloginfo(request,id):   
    blog=Blog.objects.get(id=id)
    t = loader.get_template('bloginfo.html')
    c = Context({
                 'blog':blog,
    })
    return HttpResponse(t.render(c))



def blog_index():   
    blog=Blog.objects.get(id=id)
    t = loader.get_template('bloginfo.html')
    c = Context({
                 'blog':'123',
    })
    return HttpResponse(t.render(c))


#def register(request):
#    t = loader.get_template('register.html')
#    c = Context({
#                 'employee_form': EmployeeForm(),
#    })
#    return HttpResponse(t.render(c))
    
def addstudent():
    period=2012
    banji=1
    userid=1
    student=Student.objects.get(user=userid)
    student.save()
    grade=Grade.objects.get(period=period,banji=banji)
    grade.studentlist.add(student)  
    grade.studentlist=student
    
    













